package com.yy.permissions_general.module.system.dict.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yy.permissions_general.common.config.exception.base.ServiceException;
import com.yy.permissions_general.common.constant.StatusEnum;
import com.yy.permissions_general.module.system.dict.entity.Dict;
import com.yy.permissions_general.module.system.dict.mapper.DictMapper;
import com.yy.permissions_general.module.system.dict.service.DictDataService;
import com.yy.permissions_general.module.system.dict.service.DictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 字典 服务实现类
 * </p>
 *
 * @author YY
 * @since 2021-08-08
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Resource
    private DictDataService dictDataService;
    /**
    * 分页查询列表
    * @param dict
    * @return
    */
    @Override
    public  Page<Dict>selectPageList(Dict dict, int limit, int page)  {
        Page pageContext = new Page(page,limit);
        Page<Dict> pageList = this.baseMapper.selectPage(pageContext, dict);
        return pageList;
    }

    /**
    * 新增数据
    * @param dict
    */
    @Override
    public int addDict(Dict dict) {
        dict.setDictName(dict.getDictName().trim());
        dict.setDictCode(dict.getDictCode().trim());
        int resultInt = checkDict(dict,false);
        if (resultInt == 1){
            throw new ServiceException(StatusEnum.REPEAT_ADD_ERROR);
        }
        return baseMapper.insert(dict);
    }


    /**
    * 修改数据
    * @param dict
    */
    @Override
    public int editDict(Dict dict) {
        dict.setDictName(dict.getDictName().trim());
        dict.setDictCode(dict.getDictCode().trim());
        int resultInt = checkDict(dict,true);
        if (resultInt == 1){
            throw new ServiceException(StatusEnum.REPEAT_ADD_ERROR);
        }
        return baseMapper.updateById(dict);
    }

    /**
     * 判断是否重复添加
     * @param dict
     * @param isUpdate
     * @return
     */
    private int checkDict(Dict dict, boolean isUpdate) {
        int resultInt = 0;
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<Dict>();
        if (isUpdate){
            queryWrapper.ne("id",dict.getId());
        }
        queryWrapper.eq("dict_code",dict.getDictCode());
        Dict checkDict = this.getOne(queryWrapper);
        if (checkDict != null){
            resultInt = 1;
            return resultInt;
        }
        queryWrapper = new QueryWrapper<Dict>();
        if (isUpdate){
            queryWrapper.ne("id",dict.getId());
        }
        queryWrapper.eq("dict_name",dict.getDictName());
        checkDict = this.getOne(queryWrapper);
        if (checkDict != null){
            resultInt = 1;
        }
        return resultInt;
    }
    /**
    * 查看详情
    * @param id
    * @return
    */
    @Override
    public Dict getDetailsById(Integer id) {
        return baseMapper.selectById(id);
    }

    /**
    * 删除数据
    * @param id
    * @return
    */
    @Override
    public int deleteById(Integer id) {
        dictDataService.deleteByDictId(id);
        return baseMapper.deleteById(id);
    }

    /**
    * 批量删除
    * @param ids
    * @return
    */
    @Override
    public int deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids);
    }

    /**
     * 修改状态
     * @param dict
     * @return
     */
    @Override
    public int updateDict(Dict dict) {
        return baseMapper.updateById(dict);
    }

}

